<!DOCTYPE html>
<html>
<head>
    <script>
        if (window.testRunner) {
            testRunner.dumpAsText();
            testRunner.waitUntilDone();
        }

        function testFragmentNavigation() {
            window.postMessage("postmessage", "*");
            document.querySelector('iframe').src = "http://127.0.0.1:8000/navigation/resources/postmessage-on-hashchange.html#anchor1";
            if (window.internals) {
                if (internals.doesWindowHaveUrlFragment(document.querySelector('iframe').contentWindow))
                    alert("PASS: url fragment has changed synchronously");
                else
                    alert("FAIL: url fragment should change synchronously");
            }
        }

        var receivedScheduledPostMessage = false;
        var receivedHashchangeMessage = false;
        window.addEventListener('message', function (e) {
            if (e.data === 'postmessage') {
                receivedScheduledPostMessage = true;
                if (receivedHashchangeMessage)
                    alert('FAIL: hashchange already triggered!');
                else
                    alert('PASS: scheduled postMessage() before hashchange triggered.');
            } else {
                receivedHashchangeMessage = true;
                if (receivedScheduledPostMessage)
                    alert('PASS: hashchange triggered after postMessage().');
                else
                    alert('FAIL: hashchange triggered before postMessage().');
                testRunner.notifyDone();
            }
        });

    function onloadFired() {
        alert("iframe onload fired");
        testFragmentNavigation();
    }
    </script>
</head>
<body>
    <p>This tests that same-origin fragment navigations are synchronous. It does
    so by scheduling a postMessage before scheduling the navigation. If the
    navigation is synchronous, the internals API will be able to report the presence
    of an url fragment immediately.</p>
    <iframe src="http://127.0.0.1:8000/navigation/resources/postmessage-on-hashchange.html" onload='onloadFired()'></iframe>
</body>
</html>
